const dbUtils = require('./../utils/db-util')
const poetriesService = require('./../services/poetries')

module.exports = {
    async getPoets(options) {
        if (options.poet_id) {
            let _sql = `
            SELECT * from poets
            where id = "${options.poet_id}"
            `
            let result = await dbUtils.query( _sql )
            return result
        }
        let _sql = `
        SELECT * from poets
        where name like '%${options.poet}%'
        `
        let result = await dbUtils.query( _sql )
        return result
    },
    async getPoetriesByTitle(options) {
        let page = 1
        let pageSize = 10
        if (options.page) {
            page = options.page
        }
        if (options.pageSize) {
            pageSize = options.pageSize
        }
        let start = (page - 1) * pageSize
        let length = pageSize
        
        let _sql = `
        SELECT * from poetries
        where title like '%${options.title}%' limit ${start}, ${length}
        `
        let result = await dbUtils.query( _sql )
        return result
    },
    async getPoetries(ctx) {
        let result = {
            success: false,
            message: '',
            data: null,
        }
        let poetries = await poetriesService.getPoetriesByContent(ctx.query)
        if (poetries) {
            result.data = poetries
            result.success = true
        } else {
            result.message = 'fail'
        }

        ctx.body = result
    }
}